{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'requests.models.Response'>\n",
      "200\n",
      "UTF-8\n",
      "<RequestsCookieJar[]>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "r = requests.get('http://cuiqingcai.com')\n",
    "print type(r)\n",
    "print r.status_code\n",
    "print r.encoding\n",
    "print r.cookies"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### requests的基本用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.基本get请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(\"http://httpbin.org/post\")\n",
    "r = requests.put(\"http://httpbin.org/put\")\n",
    "r = requests.delete(\"http://httpbin.org/delete\")\n",
    "r = requests.head(\"http://httpbin.org/get\")\n",
    "r = requests.options(\"http://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://httpbin.org/get?key2=value2&key1=value1\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    " \n",
    "payload = {'key1': 'value1', 'key2': 'value2'}\n",
    "r = requests.get(\"http://httpbin.org/get\", params=payload)\n",
    "print r.url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'bigberg': [7600, {1: [['iPhone', 6300], ['Bike', 800], ['shirt', 300]]}]}\n",
      "<type 'dict'>\n",
      "{\"bigberg\": [7600, {\"1\": [[\"iPhone\", 6300], [\"Bike\", 800], [\"shirt\", 300]]}]}\n",
      "<type 'str'>\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# 字典 dict 转 json 串需要使用 dumps\n",
    "\n",
    "test_dict = {'bigberg': [7600, {1: [['iPhone', 6300], ['Bike', 800], ['shirt', 300]]}]}\n",
    "print(test_dict)\n",
    "print(type(test_dict))\n",
    "#dumps 将数据转换成字符串\n",
    "json_str = json.dumps(test_dict)\n",
    "print(json_str)\n",
    "print(type(json_str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'bigberg': [7600, {u'1': [[u'iPhone', 6300], [u'Bike', 800], [u'shirt', 300]]}]}\n",
      "<type 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# json 串转 dict 字典 需要使用 loads\n",
    "\n",
    "new_dict = json.loads(json_str)\n",
    "print(new_dict)\n",
    "print(type(new_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[u'foo', u'bar', {u'foo': u'bar'}]\n"
     ]
    }
   ],
   "source": [
    "# 读取 json 文件 需要使用 load\n",
    "\n",
    "with open(\"a.json\",'r') as rj:\n",
    "    rj_dict = json.load(rj)\n",
    "    print rj_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将 json 保存到 json 文件中 使用 dump\n",
    "\n",
    "with open(\"b.json\",'w') as wj:\n",
    "    json.dump(new_dict,wj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'bigberg': [7600, {u'1': [[u'iPhone', 6300], [u'Bike', 800], [u'shirt', 300]]}]}\n",
      "{'smallberg': [8200, {1: [['Python', 81], ['shirt', 300]]}], u'bigberg': [7600, {u'1': [[u'iPhone', 6300], [u'Bike', 800], [u'shirt', 300]]}]}\n"
     ]
    }
   ],
   "source": [
    "# 将一个json文件 编辑之后 存入另一个json文件之中\n",
    "\n",
    "with open(\"b.json\",'r') as rj:\n",
    "    load_dict = json.load(rj)\n",
    "    print(load_dict)\n",
    "load_dict['smallberg'] = [8200,{1:[['Python',81],['shirt',300]]}]\n",
    "print load_dict\n",
    "\n",
    "with open(\"c.json\",'w') as wj:\n",
    "    json.dump(load_dict,wj)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
